#include <stdio.h>
#include <stdlib.h>

// 双链表节点结构
struct DListNode {
    int val;
    struct DListNode* prev;
    struct DListNode* next;
};

// 创建新节点
struct DListNode* createNode(int value) {
    struct DListNode* newNode = (struct DListNode*)malloc(sizeof(struct DListNode));
    newNode->val = value;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

// 创建链表
struct DListNode* createDLinkedList() {
    return NULL;
}

// 在头部插入
void insertAtHead(struct DListNode** head, int value) {
    struct DListNode* newNode = createNode(value);
    newNode->next = *head;
    if (*head != NULL) {
        (*head)->prev = newNode;
    }
    *head = newNode;
}

// 在尾部插入
void insertAtTail(struct DListNode** head, int value) {
    struct DListNode* newNode = createNode(value);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    struct DListNode* current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
    newNode->prev = current;
}

// 在指定位置插入
void insertAtIndex(struct DListNode** head, int index, int value) {
    if (index < 0) {
        return;
    }
    if (index == 0) {
        insertAtHead(head, value);
        return;
    }
    struct DListNode* current = *head;
    for (int i = 0; i < index - 1 && current != NULL; i++) {
        current = current->next;
    }
    if (current == NULL) {
        return;
    }
    struct DListNode* newNode = createNode(value);
    newNode->next = current->next;
    newNode->prev = current;
    if (current->next != NULL) {
        current->next->prev = newNode;
    }
    current->next = newNode;
}

// 删除头部节点
void deleteAtHead(struct DListNode** head) {
    if (*head == NULL) {
        return;
    }
    struct DListNode* temp = *head;
    *head = (*head)->next;
    if (*head != NULL) {
        (*head)->prev = NULL;
    }
    free(temp);
}

// 删除尾部节点
void deleteAtTail(struct DListNode** head) {
    if (*head == NULL) {
        return;
    }
    if ((*head)->next == NULL) {
        free(*head);
        *head = NULL;
        return;
    }
    struct DListNode* current = *head;
    while (current->next->next != NULL) {
        current = current->next;
    }
    free(current->next);
    current->next = NULL;
}

// 删除指定位置的节点
void deleteAtIndex(struct DListNode** head, int index) {
    if (index < 0 || *head == NULL) {
        return;
    }
    if (index == 0) {
        deleteAtHead(head);
        return;
    }
    struct DListNode* current = *head;
    for (int i = 0; i < index && current != NULL; i++) {
        current = current->next;
    }
    if (current == NULL) {
        return;
    }
    if (current->prev != NULL) {
        current->prev->next = current->next;
    }
    if (current->next != NULL) {
        current->next->prev = current->prev;
    }
    free(current);
}

// 查找节点
struct DListNode* find(struct DListNode* head, int value) {
    struct DListNode* current = head;
    while (current != NULL) {
        if (current->val == value) {
            return current;
        }
        current = current->next;
    }
    return NULL;
}

// 获取链表长度
int getSize(struct DListNode* head) {
    int size = 0;
    struct DListNode* current = head;
    while (current != NULL) {
        size++;
        current = current->next;
    }
    return size;
}

// 正向遍历
void traverse(struct DListNode* head) {
    struct DListNode* current = head;
    while (current != NULL) {
        printf("%d ", current->val);
        current = current->next;
    }
    printf("\n");
}

// 反向遍历
void reverseTraverse(struct DListNode* head) {
    struct DListNode* current = head;
    if (current == NULL) {
        return;
    }
    while (current->next != NULL) {
        current = current->next;
    }
    while (current != NULL) {
        printf("%d ", current->val);
        current = current->prev;
    }
    printf("\n");
}

// 清空链表
void clear(struct DListNode** head) {
    struct DListNode* current = *head;
    while (current != NULL) {
        struct DListNode* temp = current;
        current = current->next;
        free(temp);
    }
    *head = NULL;
}

// 主函数示例
int main() {
    struct DListNode* head = NULL;

    // 插入元素
    insertAtHead(&head, 1);
    insertAtTail(&head, 2);
    insertAtTail(&head, 3);

    // 正向遍历
    printf("正向遍历: ");
    traverse(head);

    // 反向遍历
    printf("反向遍历: ");
    reverseTraverse(head);

    // 获取链表长度
    printf("链表长度: %d\n", getSize(head));

    // 查找元素
    struct DListNode* node = find(head, 2);
    if (node != NULL) {
        printf("找到元素: %d\n", node->val);
    }

    // 删除元素
    deleteAtTail(&head);
    printf("删除尾部元素后的链表: ");
    traverse(head);

    // 清空链表
    clear(&head);
    printf("清空链表后的链表: ");
    traverse(head);

    return 0;
}